// pages/saveCard/saveCard.js
import {
        cardBillList,
        list
} from '../../../api/card'

Page({

        /**
         * 页面的初始数据
         */
        data: {
                haveShopCard:false,
                currentId:"", //当前的卡id
                isTimeout:false, //卡是否过期
                iconUrl: ["wallet-consumption.png", "wallet-income.png"],
                shopCardDetail:[
                        // {
                        //         isIncome:false,
                        //         icon: "wallet-consumption.png",
                        //         tag:"购物消费",
                        //         remark:"电冰箱",
                        //         account:300,
                        //         date:"05-18 20:30:20",
                        //         balance: 200
                        // },
                        // {
                        //         isIncome: true,
                        //         icon: "wallet-income.png",
                        //         tag: "充值入账",
                        //         remark: "五一节福利",
                        //         account: 500,
                        //         date: "05-18 20:30:20",
                        //         balance: 200
                        // },
                        // {
                        //         isIncome: false,
                        //         icon: "wallet-timeout.png",
                        //         tag: "购物消费",
                        //         remark: "电冰箱",
                        //         account: 300,
                        //         date: "05-18 20:30:20",
                        //         balance: 200
                        // },
                ],
                isAllshopCard:true,
                indicatorDots:"",
                shopCardInfo:[
                        // {
                        //         isAllshopCard:true,
                        //         name:"五一节五一节福利",
                        //         balance:500,
                        //         company:"中国移动莆田分公司",
                        //         tag:"全品类通用",
                        //         limitDate:"2020-05-20~2020-12-31"
                        // },
                        // {
                        //         isAllshopCard: false,
                        //         name: "五一节福利",
                        //         balance: 500,
                        //         company: "中国移动莆田分公司",
                        //         tag: "全品类通用",
                        //         limitDate: "2020-05-20~2020-12-31"
                        // },
                        // {
                        //         isAllshopCard: false,
                        //         name: "五一节福利",
                        //         balance: 500,
                        //         company: "中国移动莆田分公司",
                        //         tag: "全品类通用",
                        //         limitDate: "2020-05-20~2020-12-31"
                        // },
                ]
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                // let num = this.data.shopCardInfo.length
                // console.info(num)
                // this.setData({
                //         indicatorDots: num
                // })

        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {
                
        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
                this.getCardList()
        },

        //获取购物卡数据
        getCardList() {
                console.log("请求用户的账户卡和福利金的余额数据--")
                list({
                        cardType:1,
                        pageSize :10000
                }).then(
                        res => {
                                console.log("接收用户信息--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        if (res.data.data.length > 0){
                                                this.setData({
                                                        haveShopCard:true,
                                                        shopCardInfo: this.formatShopCardInfo(res.data.data)
                                                })
                                                let cardId = this.data.shopCardInfo[0].id
                                                if (this.data.shopCardInfo[0].timeout){
                                                        this.setData({
                                                                isTimeout: true
                                                        })
                                                }
                                                this.getCardBill(cardId)
                                        }
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        //获取账单数据
        getCardBill(cardId) {
                console.log("请求用户的福利金卡信息--")
                cardBillList({
                        cardId: cardId,
                        pageSize :10000
                }).then(
                        res => {
                                console.log("接收用户信息--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        this.setData({
                                                shopCardDetail: this.formatShopCardDetail(res.data.data)
                                        })
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        //轮播滚动
        swiperChange: function (e) {
                let shopCardInfo = this.data.shopCardInfo
                // console.info(JSON.stringify(shopCardInfo))
                // console.info(JSON.stringify(e.detail.current))
                let index = parseInt(e.detail.current)
                let id = shopCardInfo[index].id
                this.data.currentId = id
                if (this.data.shopCardInfo[index].timeout) {
                        this.setData({
                                isTimeout: true
                        })
                }else{
                        this.setData({
                                isTimeout: false
                        })
                }
                // console.info(JSON.stringify(id))
                this.getCardBill(id)
        },

        //goShopping
        goShopping(){
                console.info("this.data.currentId--" + JSON.stringify(this.data.currentId))
                let shopCardInfo = this.data.shopCardInfo
                console.info("shopCardInfo--" + JSON.stringify(shopCardInfo))
                if(this.data.isTimeout == false){
                        if (shopCardInfo.length > 0) {
                                shopCardInfo.map((item) => {
                                        if (this.data.currentId == item.id) {
                                                if (item.isAllshopCard) {
                                                        wx.switchTab({
                                                                url: '/pages/index/index',
                                                        })
                                                } else {
                                                        // let id = this.data.currentId
                                                        wx.navigateTo({
                                                                url: '/pages/member/specialArea/specialArea?tagIds=' + item.tagIds + '&name='+item.memo,
                                                        })
                                                }
                                        }
                                })
                        }
                }
        },

        //数据转化
        formatShopCardInfo(data){
                console.info("卡数据data--" + JSON.stringify(data))
                console.info("卡数据Date.now()--" + Date.now())
                if (data.length > 0){
                        data.map(item => {
                                item.timeout = false
                                if (item.endDate < Date.now()) {
                                        item.timeout = true
                                }
                                item.startDate = this.formatDate2day(item.startDate)
                                item.endDate = this.formatDate2day(item.endDate)
                                // item.companyName = "望岳科技有限公司"
                                if (item.tags.length > 0){
                                        item.tag = "仅限购买部分商品"
                                        item.isAllshopCard = false
                                }else{
                                        item.tag = "适用购买全场商品"
                                        item.isAllshopCard = true
                                }
                                item.tagIds = []
                                if(item.tags.length > 0){
                                        item.tags.map(item1 =>{
                                                item.tagIds.push(item1.id)
                                        })
                                        // item.tagIds.push(8)
                                }
                        })
                        this.data.currentId = data[0].id
                        this.setData({
                                indicatorDots: data.length
                        })
                        console.info("卡数据data--" + JSON.stringify(data))
                }
                return data
        },

        formatShopCardDetail(data) {
                console.info("卡账单数据data--" + JSON.stringify(data))
                if (data.length > 0) {
                        data.map(item => {
                                item.createDate = this.formatDate2Time(item.createDate)
                                item.result = item.credit - item.debit
                                if(item.type == 0){
                                        item.tag = ""
                                        item.isIncome = true
                                        item.icon = this.data.iconUrl[1]
                                }else
                                if (item.type == 2 ){
                                        item.tag = ""
                                        item.isIncome = true
                                        item.icon = this.data.iconUrl[1]
                                } else if (item.type == 1){
                                        item.tag = ""
                                        item.isIncome = false
                                        item.icon = this.data.iconUrl[0]
                                }
                        })
                }
                return data
        },

        //时间格式化器
        formatDate2day(val) {
                let date = new Date(val);
                let y = date.getFullYear();
                let MM = date.getMonth() + 1;
                MM = MM < 10 ? ('0' + MM) : MM;
                let d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                return y + '-' + MM + '-' + d;
        },

        formatDate2Time(val) {
                let date = new Date(val);
                let y = date.getFullYear();
                let MM = date.getMonth() + 1;
                MM = MM < 10 ? ('0' + MM) : MM;
                let d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                let h = date.getHours();
                h = h < 10 ? ('0' + h) : h;
                let m = date.getMinutes();
                m = m < 10 ? ('0' + m) : m;
                let s = date.getSeconds();
                s = s < 10 ? ('0' + s) : s;
                return y + '-' + MM + '-' + d + " " + h + ":" + m + ":" + s;
        },

})